package ljt.music.ui.playlist;

import android.app.Dialog;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

import com.ljq.mvpframework.factory.CreatePresenter;
import com.loadingdialog.WeiboDialogUtils;

import java.util.List;

import ljt.music.R;
import ljt.music.bean.PlayList;
import ljt.music.bean.SreachBean;
import ljt.music.module.base.BaseMvpActivity;
import ljt.music.ui.PlayMusicActivity;
import ljt.music.utils.StatusBarUtils;
import ljt.music.widget.CircleImageFragment;
import ljt.music.widget.PlayDialog;

/**
 * @author jiangtao
 */
@CreatePresenter(PlayListPresenter.class)
public class PlayListActivity extends BaseMvpActivity<PlayListView, PlayListPresenter> implements PlayListView,
    PlayListAdapter.OnItemClickLitener ,CircleImageFragment.OnFragmentInteractionListener {
  private final String TAG = "PlayListActivity";
  private RecyclerView recycler_view;
  private PlayListAdapter adapter;
  private Dialog loadingDialog;
  private String id;
  private String name;
  private Toolbar toolbar;

  //getMvpPresenter().request("934013820", "playlist");
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_play_list);

    initView();
    initData();
  }

  private void initData() {
    Intent intent = getIntent();
    if (intent != null) {
      String types = intent.getStringExtra("types");
      id = intent.getStringExtra("id");
      String source = intent.getStringExtra("source");

      String count = intent.getStringExtra("count");
      String pages = intent.getStringExtra("pages");
      name = intent.getStringExtra("name");
      Log.i(TAG, "types:" + types);
      Log.i(TAG, "id:" + id);
      Log.i(TAG, "source:" + source);
      if (id != null) {
        getMvpPresenter().request(id, types);
      } else if (name != null) {
        getMvpPresenter().search(types, count, source, pages, name);
      }
    }
  }

  private void initView() {
    StatusBarUtils.setStatusBarDarkInUiThread(this, false);

    toolbar = findViewById(R.id.toolbar);
    toolbar.setTitle("音乐");
    setSupportActionBar(toolbar);
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    recycler_view = findViewById(R.id.recycler_view);
    recycler_view.setNestedScrollingEnabled(false);
    recycler_view.setLayoutManager(new LinearLayoutManager(this));
    recycler_view.setAdapter(adapter = new PlayListAdapter(this));
    recycler_view.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.VERTICAL));

    adapter.setOnItemClickLitener(this);
  }

  @Override
  public void requestLoading() {
    loadingDialog = WeiboDialogUtils.createLoadingDialog(this, "请稍后...");
  }

  @Override
  public void resultSuccess(PlayList result) {
    adapter.setPlayList(result);
    WeiboDialogUtils.closeDialog(loadingDialog);
    Log.i(TAG, "PlayList0:" + result.toString());
  }

  @Override
  public void searchSuccess(List<SreachBean> result) {
    adapter.setSearchList(result);
  }

  @Override
  public void resultFailure(String result) {
    WeiboDialogUtils.closeDialog(loadingDialog);
    Toast.makeText(this, "请求失败 " + result, Toast.LENGTH_SHORT).show();
  }

  @Override
  public void onItemClick(View view, int position) {
    PlayList playList = adapter.getPlayList();
    List<SreachBean> searchList = adapter.getSearchList();

    String name = "";
    String id = "";
    String subtitle = "";
    String url = "";

    if (playList != null) {
      PlayList.PlaylistBean playlist = playList.getPlaylist();
      if (playlist != null) {
        List<PlayList.PlaylistBean.TracksBean> tracks = playlist.getTracks();
        PlayList.PlaylistBean.TracksBean tracksBean = tracks.get(position);
        name = tracksBean.getName();
        id = String.valueOf(tracksBean.getId());
        subtitle = tracksBean.getAr().get(0).getName();
        url = tracksBean.getAl().getPicUrl();
      }
    } else if (searchList != null) {
      SreachBean sreachBean = searchList.get(position);
      name = sreachBean.getName();
      id = String.valueOf(sreachBean.getId());
      subtitle = sreachBean.getAlbum();
      url = String.valueOf(sreachBean.getUrl_id());
    }

//    PlayDialog playDialog = new PlayDialog(this, name, id, subtitle, url);
//    playDialog.showDialog();

    Intent intent = new Intent(this, PlayMusicActivity.class);
    intent.putExtra("name",name);
    intent.putExtra("id",id);
    intent.putExtra("subtitle",subtitle);
    intent.putExtra("url",url);
    startActivity(intent);
  }

  @Override
  public void onItemLongClick(View view, int position) {

  }

  @Override
  public void onFragmentInteraction(Uri uri) {
    //
  }
}
